package mainFragment.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import common.BaseFragment;
import common.Constant;
import https.InterNetController;
import mainFragment.adapter.ZhaoShengMessageListAdapter;
import mainFragment.bean.ZhaoShengMessageBean;

/**
 * Created by Administrator on 2017/3/8.
 * 里面的listview是普通listview,用于首页显示
 */
@SuppressLint("ValidFragment")
public class ZhaoShengMessageListView extends BaseFragment {
    private View view;
    private Context context;
    private String id;
    private ListView college_fragment_zsm_lv;
    private ZhaoShengMessageListAdapter adapter;
    private ArrayList<ZhaoShengMessageBean.NewsListBean> arrayList;
    private ZhaoShengMessageBean zhaoShengMessageBean;


    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    Gson gson = new Gson();
                    zhaoShengMessageBean = gson.fromJson((String) msg.obj, ZhaoShengMessageBean.class);
                    if (zhaoShengMessageBean != null) {
                        if(arrayList!=null) {
                            List<ZhaoShengMessageBean.NewsListBean> newsList = zhaoShengMessageBean.getNewsList();
                            if (newsList != null) {
                                for (int i = 0; i < newsList.size(); i++) {
                                    arrayList.add(newsList.get(i));
                                }
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    };


    public ZhaoShengMessageListView(Context context, String id) {
        this.id = id;
        this.context = context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.college_fragment_zsm_lv, null, false);
        }
        if (getMyActivity() != null) {
            initView();
        }
        return view;
    }

    private void initView() {
        arrayList = new ArrayList<>();
        college_fragment_zsm_lv = (ListView) view.findViewById(R.id.college_fragment_zsm_lv);
        adapter = new ZhaoShengMessageListAdapter(getMyActivity(), arrayList);
        college_fragment_zsm_lv.setAdapter(adapter);
    }

    public void setZhaoShengMessageBean(ZhaoShengMessageBean collegeBean) {
        if(collegeBean == null){
            getZhaoShengMessageFromInternet(1);
        }
        else{
            if(arrayList!=null) {
                List<ZhaoShengMessageBean.NewsListBean> newsList = collegeBean.getNewsList();
                if (newsList != null) {
                    for (int i = 0; i < newsList.size(); i++) {
                        arrayList.add(newsList.get(i));
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }

    private void getZhaoShengMessageFromInternet(int page) {
        if (id != null) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("college_id", id);
            hashMap.put("page", String.valueOf(page));
            new InterNetController(getMyActivity(), Constant.NEWLIST, myHandler, hashMap, 0,false);
        }
    }
}
